"""数据同步工具使用示例"""

import sys
import os

# 添加项目路径
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
sys.path.insert(0, os.path.join(project_root, "src"))

from tools.data_sync import SyncConfig, DataSyncTool, data_sync_tool


def example_usage():
    """数据同步工具使用示例"""
    
    # 方式1: 直接使用工具函数
    print("=== 使用工具函数 ===")
    
    # 查看状态
    result = data_sync_tool(
        action="status"
    )
    print(f"状态查询结果: {result}")
    
    # 执行一次同步
    result = data_sync_tool(
        action="sync_once",
        sql_server_host="localhost",
        sql_server_database="YourDatabase",
        sql_server_username="your_username",
        sql_server_password="your_password",
        tables_to_sync=["Users", "Orders", "Products"]
    )
    print(f"同步结果: {result}")
    
    # 启动定时同步
    result = data_sync_tool(
        action="start",
        sql_server_host="localhost",
        sql_server_database="YourDatabase",
        sql_server_username="your_username",
        sql_server_password="your_password",
        sync_interval_minutes=60
    )
    print(f"启动定时同步: {result}")
    
    # 方式2: 直接使用类
    print("\n=== 使用类接口 ===")
    
    config = SyncConfig(
        sql_server_host="localhost",
        sql_server_database="YourDatabase",
        sql_server_username="your_username",
        sql_server_password="your_password",
        tables_to_sync=["Users", "Orders"],
        sync_interval_minutes=30
    )
    
    sync_tool = DataSyncTool(config)
    
    # 执行一次同步
    try:
        sync_tool.perform_sync()
        print("同步完成")
    except Exception as e:
        print(f"同步失败: {e}")
    
    # 启动定时同步
    sync_tool.start_scheduled_sync()
    print("定时同步已启动")
    
    # 查看状态
    status = sync_tool.get_sync_status()
    print(f"同步状态: {status}")
    
    # 停止同步
    sync_tool.stop_scheduled_sync()
    print("定时同步已停止")


if __name__ == "__main__":
    example_usage()